//
//  YDYCleanCacheCell.m
//  DYSisterAPP
//
//  Created by 杨德勇 on 16/8/4.
//  Copyright © 2016年 杨德勇. All rights reserved.
//

#import "YDYCleanCacheCell.h"
#import "NSString+YDYExtension.h"
#import <SVProgressHUD.h>
#import <SDImageCache.h>

@implementation YDYCleanCacheCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        //把自己变成弱引用，让cell即时销毁。如果不这样写在遍历的时候控制器销毁了，但是cell还在，知道代码块里面的所有内容执行完成之后再销毁，这部分资源浪费了。用弱引用的话，控制器销毁了cell就销毁了，也就不会执行后面的代码了，也防止出现野指针。
        __weak typeof (self) weakself = self;
        
        weakself.textLabel.text = @"正在计算缓存.....";
        
        UIActivityIndicatorView *loadview = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        weakself.accessoryView = loadview;
        //关闭交互，防止在计算的时候被用户点击,放在文字下面，不然字就变灰色了。
        weakself.userInteractionEnabled = NO;
        [loadview startAnimating];
        
        
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
       
        //获取文件大小
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            if (weakself == nil) {
                return ;
            }
            
            NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES).lastObject;
            
            NSString *dirpath = [cachePath stringByAppendingPathComponent:@"default"];
            //生成文字
            CGFloat size = dirpath.ydy_fileSize;
            NSString *text = [NSString stringWithFormat:@"清理缓存(%.2fMB)",size / 1000000];
            
            //回到主线程
            dispatch_async(dispatch_get_main_queue(), ^{
                weakself.textLabel.text = text;
                //清清除菊花按键
                weakself.accessoryView = nil;
                weakself.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                //恢复交互
                weakself.userInteractionEnabled = YES;
                //让它计算完成之后才加入清除功能
                 [weakself addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clearCache)]];
            });
//        });
       });
        
    }
    //添加点击事件
    
   
    return self;
}

-(void)clearCache{
    //弹出指示器
    [SVProgressHUD showWithStatus:@"正在清除缓存"];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    //删除出webimage的图片之后
    [[SDImageCache sharedImageCache]clearDiskOnCompletion:^{
        //删除自定义缓存
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            NSFileManager *mgr = [NSFileManager defaultManager];
            [mgr removeItemAtPath:@"这里写入自己的路径" error:nil];
            [mgr createDirectoryAtPath:@"这里写入删除之后要重新建立的文件夹的路径" withIntermediateDirectories:YES attributes:nil error:nil];
            
            //删除完成之后回到主线程
            dispatch_async(dispatch_get_main_queue(), ^{
                //模仿删除延迟操作
#warning 这里清理内存用了延时delay 1s
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    //清除完成之后
                    [SVProgressHUD dismiss];
                    self.textLabel.text = [NSString stringWithFormat:@"清理缓存(0MB)"];
                });
            });
            
        });
        
       
    }];
}
-(void)layoutSubviews{
    [super layoutSubviews];
    //当cell重新出现的时候会来这个方法
    //重启菊花视图。
    [(UIActivityIndicatorView *)self.accessoryView startAnimating];
}
@end
